package schedulo.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.IOException;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.border.TitledBorder;

import schedulo.core.task.provider.Spec;
import schedulo.core.task.provider.TaskProvider;
import schedulo.core.task.provider.TaskProviderFactory;
import schedulo.core.task.reminder.Reminder;
import schedulo.gui.components.ScheduleTable;
import schedulo.gui.components.TaskEditorTab;
import schedulo.gui.components.TimeTrackerPanel;
import schedulo.gui.components.TimeTrackerTab;

public class ScheduloGui extends JFrame {

	private JPanel contentPanel = null;

	private JSplitPane mainHorizontalSplitPane = null;
	
	private ScheduleTable scheduleTable = null;

	private JTabbedPane tabbedPane = null;

	private JSplitPane taskListsFiltersSplitPane = null;

	private JPanel taskListsPanel = null;

	private JPanel filterPanel = null;

	private JScrollPane taskListScrollPane = null;

	private JList taskLists = null;

	private JScrollPane filtersScrollPane = null;

	private JList filtersList = null;

	private TaskProviderListModel taskProviderListModel;

	private JMenuBar jJMenuBar = null;

	private JMenu jMenu_TaskLists = null;

	private JMenuItem jMenuItem_AddTaskProvider = null;

	private JMenuItem jMenuItem_SaveTaskProviders = null;

	private TimeTrackerTab timeTrackerTab;

	/**
	 * This method initializes
	 * 
	 */
	public ScheduloGui() {
		super();

		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {

		// instanciate the reminder tool
		new ReminderGui(); 
		
		taskProviderListModel = new TaskProviderListModel();

		this.setSize(new Dimension(800, 550));
		this.setJMenuBar(getJJMenuBar());
		this.setTitle("Schedulo - Time Manager");
		this.setContentPane(getContentPanel());
	}

	/**
	 * This method initializes contentPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getContentPanel() {
		if (contentPanel == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			contentPanel = new JPanel();
			contentPanel.setPreferredSize(new Dimension(400, 300));
			contentPanel.setLayout(new GridBagLayout());
			contentPanel.add(getMainHorizontalSplitPane(), gridBagConstraints);
		}
		return contentPanel;
	}

	/**
	 * This method initializes jSplitPane
	 * 
	 * @return javax.swing.JSplitPane
	 */
	private JSplitPane getMainHorizontalSplitPane() {
		if (mainHorizontalSplitPane == null) {
			mainHorizontalSplitPane = new JSplitPane();
			mainHorizontalSplitPane.setPreferredSize(new Dimension(290, 150));
			mainHorizontalSplitPane.setRightComponent(getTabbedPane());
			mainHorizontalSplitPane.setLeftComponent(getTaskListsFiltersSplitPane());
		}
		return mainHorizontalSplitPane;
	}

	/**
	 * This method initializes editorTabs
	 * 
	 * @return javax.swing.JTabbedPane
	 */
	private JTabbedPane getTabbedPane() {
		if (tabbedPane == null) {
			tabbedPane = new JTabbedPane();
			//tabbedPane.addTab("Schedule view", getScheduleTable());
			tabbedPane.addTab("Time tracker", getTimeTrackerTab());
		}
		return tabbedPane;
	}
	
	private ScheduleTable getScheduleTable() {
		if(scheduleTable == null) {
			scheduleTable = new ScheduleTable();
		}
		return scheduleTable;
	}
	
	private TimeTrackerTab getTimeTrackerTab() {
		if (timeTrackerTab == null) {
			timeTrackerTab = new TimeTrackerTab();
		}
		return timeTrackerTab;
	}

	/**
	 * This method initializes taskListsFiltersSplitPane
	 * 
	 * @return javax.swing.JSplitPane
	 */
	private JSplitPane getTaskListsFiltersSplitPane() {
		if (taskListsFiltersSplitPane == null) {
			taskListsFiltersSplitPane = new JSplitPane();
			taskListsFiltersSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
			taskListsFiltersSplitPane.setTopComponent(getTaskListsPanel());
			taskListsFiltersSplitPane.setBottomComponent(getFilterPanel());
			taskListsFiltersSplitPane.setResizeWeight(0.5);
		}
		return taskListsFiltersSplitPane;
	}

	/**
	 * This method initializes taskListsPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTaskListsPanel() {
		if (taskListsPanel == null) {
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.BOTH;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.weighty = 1.0;
			taskListsPanel = new JPanel();
			taskListsPanel.setLayout(new GridBagLayout());
			taskListsPanel.setBorder(BorderFactory.createTitledBorder(null,
					"Task lists", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, null, null));
			taskListsPanel.setMinimumSize(new Dimension(100, 100));
			taskListsPanel.add(getTaskListScrollPane(), gridBagConstraints3);

		}
		return taskListsPanel;
	}

	/**
	 * This method initializes filterPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getFilterPanel() {
		if (filterPanel == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.weighty = 1.0;
			filterPanel = new JPanel();
			filterPanel.setLayout(new GridBagLayout());
			filterPanel.setBorder(BorderFactory.createTitledBorder(null,
					"Filters", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, null, null));
			filterPanel.setMinimumSize(new Dimension(100, 100));
			filterPanel.add(getFiltersScrollPane(), gridBagConstraints1);
		}
		return filterPanel;
	}

	/**
	 * This method initializes taskListScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getTaskListScrollPane() {
		if (taskListScrollPane == null) {
			taskListScrollPane = new JScrollPane();
			taskListScrollPane.setViewportView(getTaskLists());
		}
		return taskListScrollPane;
	}

	/**
	 * This method initializes taskList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getTaskLists() {
		if (taskLists == null) {
			taskLists = new JList(taskProviderListModel);
			// taskLists.setCellRenderer(new TaskProviderListCellRenderer());
		}
		return taskLists;
	}

	/**
	 * This method initializes filtersScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getFiltersScrollPane() {
		if (filtersScrollPane == null) {
			filtersScrollPane = new JScrollPane();
			filtersScrollPane.setViewportView(getFiltersList());
		}
		return filtersScrollPane;
	}

	/**
	 * This method initializes filtersList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getFiltersList() {
		if (filtersList == null) {
			filtersList = new JList();
		}
		return filtersList;
	}

	/**
	 * This method initializes jJMenuBar
	 * 
	 * @return javax.swing.JMenuBar
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJMenu_TaskLists());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJMenu_TaskLists() {
		if (jMenu_TaskLists == null) {
			jMenu_TaskLists = new JMenu();
			jMenu_TaskLists.setText("Task Lists");
			jMenu_TaskLists.add(getJMenuItem_AddTaskProvider());
			jMenu_TaskLists.add(getJMenuItem_SaveTaskProviders());
		}
		return jMenu_TaskLists;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItem_AddTaskProvider() {
		if (jMenuItem_AddTaskProvider == null) {
			jMenuItem_AddTaskProvider = new JMenuItem();
			jMenuItem_AddTaskProvider.setText("Add task list ...");
			jMenuItem_AddTaskProvider
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {

							String str = JOptionPane
									.showInputDialog(
											ScheduloGui.this,
											"Enter the provider definition for the new list:",
											"File:filename=/home/topaz/tasks.task"
									/*
									 * "Task Lists > Add list ...",
									 * JOptionPane.PLAIN_MESSAGE
									 */);
							if (str != null) {
								try {
									TaskProvider provider = TaskProviderFactory
											.getInstance(new Spec(str));

									// add task editor tab
									getTabbedPane().addTab(str, null,
											new TaskEditorTab(provider));

								} catch (Exception e1) {
									e1.printStackTrace();
									JOptionPane
											.showMessageDialog(
													ScheduloGui.this,
													"Cannot instanciate a task provider with definition: '"
															+ str + "'",
													"Task Lists > Add list ... > ERROR",
													JOptionPane.ERROR_MESSAGE);
								}
							}
						}
					});
		}
		return jMenuItem_AddTaskProvider;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItem_SaveTaskProviders() {
		if (jMenuItem_SaveTaskProviders == null) {
			jMenuItem_SaveTaskProviders = new JMenuItem();
			jMenuItem_SaveTaskProviders.setText("Save all task lists");
			jMenuItem_SaveTaskProviders
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							Iterator<TaskProvider> itTP = taskProviderListModel
									.getTaskProviders().iterator();
							while (itTP.hasNext()) {
								TaskProvider tp = itTP.next();
								try {
									tp.save();
								} catch (IOException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
							}
						}
					});
		}
		return jMenuItem_SaveTaskProviders;
	}

	public static void main(String[] args) {

		// try {
		// UIManager.setLookAndFeel(new WindowsClassicLookAndFeel());
		// } catch (UnsupportedLookAndFeelException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

		ScheduloGui sg = new ScheduloGui();
		sg.setDefaultCloseOperation(sg.EXIT_ON_CLOSE);
		sg.setVisible(true);
	}
}
